package com.shenhan.unsafe;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.IntStream;

/**
 * @author shenhan
 * @create 2020/6/10 17:41
 * @description
 */
public class MapTest {
    /**
     * HashMap并发不安全解决方案：
     * 1、Collections.synchronizedMap(new HashMap<>(30))
     * 2、new ConcurrentHashMap<>(30)
     * @param args
     */
    public static void main(String[] args) {
//        Map<String, String> map = new HashMap<>(30);
//        Map<String, String> map = Collections.synchronizedMap(new HashMap<>(30));
        Map<String, String> map = new ConcurrentHashMap<>(30);
        IntStream.range(1, 30).forEach(i -> new Thread(() -> {
            map.put(Thread.currentThread().getName(), UUID.randomUUID().toString().substring(0, 6));
            System.out.println(map);
        }, String.valueOf(i)).start());
    }
}
